package com.xyz.keshe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.xyz.keshe.R;
import com.xyz.keshe.model.Category;

import java.util.List;

public class CategoryManageAdapter extends BaseAdapter {

    private Context context;
    private List<Category> categories;
    private OnCategoryActionListener listener;

    public interface OnCategoryActionListener {
        void onEdit(Category category);
        void onDelete(Category category);
    }

    public CategoryManageAdapter(Context context, List<Category> categories, OnCategoryActionListener listener) {
        this.context = context;
        this.categories = categories;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return categories.size();
    }

    @Override
    public Object getItem(int position) {
        return categories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return categories.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_category_manage, parent, false);
        }

        Category category = categories.get(position);

        TextView tvName = convertView.findViewById(R.id.tv_category_name);
        TextView tvDescription = convertView.findViewById(R.id.tv_category_description);
        Button btnEdit = convertView.findViewById(R.id.btn_edit_category);
        Button btnDelete = convertView.findViewById(R.id.btn_delete_category);

        tvName.setText(category.getName());
        tvDescription.setText(category.getDescription());

        btnEdit.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEdit(category);
            }
        });

        btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDelete(category);
            }
        });

        return convertView;
    }
} 